package cn.lingyangwl.agile.gateway.remote;

import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.core.utils.spring.*;
import cn.lingyangwl.framework.tool.core.exception.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;

/**
 * @author shenguangyang
 */
@Service
public class AuthApi {

    @Async
    public Future<Map<String, List<String>>> listClientPermission(String clientId) {
        // 解决gateway使用feign时, 启动卡住问题, 这里不采用属性注入, 使用工具类动态获取
        AuthRemote service = SpringUtils.getBean(AuthRemote.class);
        Resp<Map<String, List<String>>> resp = service.listClientPermission(clientId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));

        Map<String, List<String>> data = Optional.ofNullable(resp.getData()).orElse(new ConcurrentHashMap<>());
        return new AsyncResult<>(data);
    }
}
